package com.ttc.group2.videochat.activity;

import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.utils.DialogUtils;
import com.ttc.group2.videochat.view.TitleBar;

public class ChatActivity extends BaseActivity implements OnClickListener {
	private ImageView imgBack, imgCancel;
	private TitleBar title, titleLand;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_conversation);

		// custom ActionBar
		customActionBar();
	}

	private void customActionBar() {
		mActionbar = getSupportActionBar();
		mActionbar.setDisplayShowHomeEnabled(false);
		mActionbar.setDisplayShowTitleEnabled(false);
		mActionbar.setSplitBackgroundDrawable(new ColorDrawable(Color
				.parseColor("#ff00aff0")));
		LayoutInflater inflater = LayoutInflater.from(this);
		View customView = inflater.inflate(R.layout.custom_actionbar, null);
		imgBack = (ImageView) customView.findViewById(R.id.custom_action_back);
		imgCancel = (ImageView) customView
				.findViewById(R.id.custom_action_cancel);
		title = (TitleBar) customView.findViewById(R.id.custom_title);
		titleLand = (TitleBar) customView
				.findViewById(R.id.custom_title_landscape);
		title.title.setText("People name");

		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			titleLand.setVisibility(View.VISIBLE);
			title.setVisibility(View.GONE);
			titleLand.title.setText("People name");
		}

		imgBack.setOnClickListener(this);
		imgCancel.setOnClickListener(this);

		mActionbar.setCustomView(customView);
		mActionbar.setDisplayShowCustomEnabled(true);
	}

	@Override
	public void onClick(View v) {
		int i=v.getId(); 
		if(i==R.id.custom_action_back){
			finish();
		}else if(i==R.id.custom_action_cancel){
			finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_view_chat, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int i=item.getItemId();
		if(i==R.id.menu_video_call){
			DialogUtils.showToast(this, "Video call", Toast.LENGTH_SHORT);
			return true;
		}
		else if(i==R.id.menu_view_profile){
			DialogUtils.showToast(this, "View profile", Toast.LENGTH_SHORT);
			return true;
		}
		else if(i==R.id.settings){
			DialogUtils.showToast(this, "Settings", Toast.LENGTH_SHORT);
			return true;
		}
		else if(i==R.id.menu_item_sign_out){
			DialogUtils.showToast(this, "Sign out", Toast.LENGTH_SHORT);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			toggleActionBar();
		}
		return super.onTouchEvent(event);
	}

	private void toggleActionBar() {
		if (mActionbar != null) {
			if (mActionbar.isShowing()) {
				mActionbar.hide();
			} else {
				mActionbar.show();
			}
		}
	}
}
